package cn.maihe.elg.operation.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;

/**
 * @author JHWei
 * @date 2018/7/23
 */
public class StringUtil extends StringUtils {

    public static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * byte数组转字符串
     *
     * @param src
     * @return
     */
    public static String byte2String(byte[] src) {
        return byte2String(src, DEFAULT_CHARSET);
    }

    /**
     * byte数组转字符串
     *
     * @param src
     * @param charset
     * @return
     */
    public static String byte2String(byte[] src, String charset) {
        String res = null;
        try {
            if (src == null) {
                res = "";
            } else {
                if (StringUtils.isBlank(charset)) {
                    charset = DEFAULT_CHARSET;
                }

                res = new String(src, charset);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("byte数组转String异常");
        }
        return res;
    }

    /**
     * 字符串转byte数组
     *
     * @param src
     * @return
     */
    public static byte[] string2Byte(String src) {
        return string2Byte(src, DEFAULT_CHARSET);
    }

    /**
     * 字符串转byte数组
     *
     * @param src
     * @param charset
     * @return
     */
    public static byte[] string2Byte(String src, String charset) {
        byte[] res = null;
        try {
            if (src == null) {
                src = "";
            }

            if (StringUtils.isBlank(charset)) {
                charset = DEFAULT_CHARSET;
            }

            res = src.getBytes(charset);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("字符串转byte数组异常");
        }
        return res;
    }

    /**
     * 转换string
     *
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        String res = "";
        if (obj != null) {
            res = String.valueOf(obj);
        }
        return res;
    }

    public static String unicode2utf8(String unionString) {
        char aChar;
        int len = unionString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = unionString.charAt(x++);
            if (aChar == '\\') {
                aChar = unionString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = unionString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else {
                outBuffer.append(aChar);
            }
        }
        return outBuffer.toString();
    }

    public static String toYuan(Long amt) {
        return toYuan(String.valueOf(amt));
    }

    public static String toYuan(String amt) {
        try {
            BigDecimal bigDecimal = new BigDecimal(amt);
            return bigDecimal.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).toString();
        } catch (Exception e) {
        }
        return null;
    }

    public static String fen2YuanCurrency(Long amt) {
        try {
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
            return numberFormat.format(Double.valueOf(toYuan(amt)));
        } catch (Exception e) {
        }
        return null;
    }

    public static long yuanToFen(String yuan) {
        try {
            BigDecimal bigDecimal = new BigDecimal(yuan);
            return bigDecimal.multiply(BigDecimal.valueOf(100)).longValue();
        } catch (Exception e) {
            throw new RuntimeException("元转分异常:" + e.getMessage(), e);
        }
    }

    public static String genGuaranteeCheckNo() {
        int r = new Random().nextInt(99999999);
        if (r < 100000) {
            return String.format("%06s", r);
        }
        return String.valueOf(r).substring(0, 6);
    }

    public static String divide(String source,int scale){
       return new BigDecimal(source).divide(BigDecimal.valueOf(100L),scale, RoundingMode.HALF_UP).toString();
    }

    public static String multiplyTen(String source,int scale){
        return new BigDecimal(source).multiply(BigDecimal.valueOf(10L)).setScale(scale,RoundingMode.HALF_UP).toString();
    }

    /**
     * 快速去除所有空格
     * @param input
     * @return
     */
    public static String removeAllWhitespaceFast(String input) {
        if (input == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (!Character.isWhitespace(c)) {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        System.out.println("s = " + StringUtil.divide("0.15",6));
    }
}
